package com.xxx.meter.model.data.meter;

import com.google.gson.Gson;
import com.xxx.meter.model.http.bean.MachineChildBean;
import com.xxx.meter.model.http.bean.MachineInfoBean;
import com.xxx.meter.model.http.vo.MachineSubmitVo;
import com.xxx.meter.model.http.bean.SubmitBean;
import com.xxx.meter.model.sp.SharedConst;
import com.xxx.meter.model.sp.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 抄表操作工具类
 */

public class MeterUtil {

    private static Gson gson = new Gson();

    /**
     * 获取到本地暂存设备列表
     *
     * @param machineType 设备类型
     * @return List<MachineChildBean>   设备集合
     */
    public static List<MachineChildBean> getMachineChildList(String machineType) {
        List<MeterEntry.DataItemEntry> list = pauseList();
        ArrayList<MachineChildBean> dataList = new ArrayList<>();
        if (machineType == null) {
            return dataList;
        }
        for (int i = 0; i < list.size(); i++) {
            MeterEntry.DataItemEntry dataEntry = list.get(i);
            MachineChildBean machineChildBean = dataEntry.getMachineChildBean();
            if (machineType.equals(machineChildBean.getType())) {
                dataList.add(machineChildBean);
            }
        }
        return dataList;
    }


    /**
     * 获取每一个设备列表对应的信息表
     *
     * @param machineId 设备Id
     * @return List<MachineInfoBean>   信息集合
     */
    public static List<MachineInfoBean> getMachineInfoList(String machineType, String machineId) {
        List<MeterEntry.DataItemEntry> list = pauseList();

        ArrayList<MachineInfoBean> dataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MeterEntry.DataItemEntry dataEntry = list.get(i);
            if (machineId.equals(dataEntry.getMachineChildBean().getId()) &&
                    machineType.equals(dataEntry.getMachineChildBean().getType())
                    ) {
                dataList.addAll(dataEntry.getMachineInfoBeanList());
                break;
            }
        }
        return dataList;
    }

    /**
     * 扫描后植入本地
     *
     * @param machineChildBean 设备对象
     */
    public static MeterEntry.DataItemEntry createMachineChild(MachineChildBean machineChildBean) {
        List<MeterEntry.DataItemEntry> list = pauseList();

        //判断是否已存在
        for (MeterEntry.DataItemEntry dataEntry : list) {
            MachineChildBean dataMachineChildBean = dataEntry.getMachineChildBean();
            if (dataMachineChildBean.getType().equals(machineChildBean.getType())
                    && dataMachineChildBean.getId().equals(machineChildBean.getId())) {
                return dataEntry;
            }
        }

        MeterEntry.DataItemEntry dataEntry = new MeterEntry.DataItemEntry();
        dataEntry.setMachineChildBean(machineChildBean);
        list.add(dataEntry);
        saveList(list);
        return null;
    }

    /**
     * 保存修改信息
     *
     * @param machineType 设备Type
     * @param machineId   设备Id
     * @param infoBeans   设备信息列表
     */
    public static void updateMachineInfo(String machineType, String machineId, List<MachineInfoBean> infoBeans) {
        List<MeterEntry.DataItemEntry> list = pauseList();

        for (int i = 0; i < list.size(); i++) {
            MeterEntry.DataItemEntry dataEntry = list.get(i);
            if (dataEntry.getMachineChildBean().getId().equals(machineId) &&
                    dataEntry.getMachineChildBean().getType().equals(machineType)) {
                dataEntry.setMachineInfoBeanList(infoBeans);
                list.set(i, dataEntry);
                break;
            }
        }
        saveList(list);
    }

    /**
     * 获取提交数据
     *
     * @param userName 用户名
     */
    public static String getSubmitData(List<SubmitBean> userName) {
        MeterEntry dataEntry = pauseUser();
        if (dataEntry == null) {
            return null;
        }
        List<String> userNameList = dataEntry.getUserName();
        for (SubmitBean submitBean : userName) {
            userNameList.add(submitBean.getName());
        }
        dataEntry.setUserName(userNameList);

        //TODO 转化格式
        MachineSubmitVo machineSubmitVo = new MachineSubmitVo();
        List<MachineSubmitVo.DataVo> submitList = new ArrayList<>();
        List<MeterEntry.DataItemEntry> data = dataEntry.getData();
        for (int i = 0; i < data.size(); i++) {
            MeterEntry.DataItemEntry dataItemEntry = data.get(i);
            List<MachineInfoBean> machineInfoBeanList = dataItemEntry.getMachineInfoBeanList();
            String type = dataItemEntry.getMachineChildBean().getType();
            String id = dataItemEntry.getMachineChildBean().getId();
            for (int i1 = 0; i1 < machineInfoBeanList.size(); i1++) {
                MachineInfoBean machineInfoBean = machineInfoBeanList.get(i1);
                MachineSubmitVo.DataVo dataVo = new MachineSubmitVo.DataVo();
                dataVo.setEquipmenttypekey(type);
                dataVo.setEquipmentkey(id);
                dataVo.setId(machineInfoBean.getId());
                dataVo.setItemvalue(machineInfoBean.getData());
                submitList.add(dataVo);
            }
        }
        machineSubmitVo.setSubmitBack(dataEntry.getSubmitBack());
        machineSubmitVo.setUserName(dataEntry.getUserName());
        machineSubmitVo.setData(submitList);
        return gson.toJson(machineSubmitVo);
    }

    /**
     * 缓存提交说明
     *
     * @param back 设备信息列表
     */
    public static void saveSubmitData(String back) {
        MeterEntry dataEntry = pauseUser();
        if (dataEntry != null) {
            dataEntry.setSubmitBack(back);
        }
        saveUser(dataEntry);
    }

    /**
     * 获取缓存提交说明
     */
    public static String getSubmitData() {
        MeterEntry dataEntry = pauseUser();
        if (dataEntry == null) {
            return "";
        }
        return dataEntry.getSubmitBack();
    }

    //是否有过巡检记录
    public static boolean isCheck() {
        return SharedPreferencesUtil.getInstance().get(SharedConst.METER) != null;
    }

    //提交后清空训机
    public static void cleanMachine() {
        SharedPreferencesUtil.getInstance().save(SharedConst.METER, null);
    }

    //解析用户
    private static MeterEntry pauseUser() {
        String data = SharedPreferencesUtil.getInstance().get(SharedConst.METER);
        MeterEntry dataEntry = gson.fromJson(data, MeterEntry.class);
        return dataEntry == null ? new MeterEntry() : dataEntry;
    }

    //解析集合
    private static List<MeterEntry.DataItemEntry> pauseList() {
        try {
            MeterEntry dataEntry = pauseUser();
            List<MeterEntry.DataItemEntry> list = dataEntry.getData();
            return list == null ? new ArrayList<MeterEntry.DataItemEntry>() : list;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    //保存集合
    private static void saveList(List<MeterEntry.DataItemEntry> list) {
        MeterEntry dataEntry = pauseUser();
        dataEntry.setData(list);
        SharedPreferencesUtil.getInstance().save(SharedConst.METER, gson.toJson(dataEntry));
    }

    //保存用户
    private static void saveUser(MeterEntry dataEntry) {
        SharedPreferencesUtil.getInstance().save(SharedConst.METER, gson.toJson(dataEntry));
    }

}
